#include "init.h"
#include <stdio.h>

int getConnectionConfig(connection_config * stConnConfig);
static const char * configFileName = "/home/yt/socket/demo_algrithm/config.ini";

class CConfig * CConfig::m_instance = NULL;

CConfig::CConfig()
{
	m_pstConnectionConfig = new connection_config;
	getConnectionConfig(m_pstConnectionConfig);
}

class CConfig * CConfig::getGlobleConfig(void)
{
     if(m_instance == NULL)
     {
    	 m_instance = new CConfig();
     }
     return m_instance;
}

connection_config* CConfig::getConnConfig(void)
{
    return m_pstConnectionConfig;
}

CConfig::~CConfig()
{
   if(m_pstConnectionConfig)
   {
	   delete m_pstConnectionConfig;
	   m_pstConnectionConfig = NULL;
   }

   /*if(m_instance)
   {
       delete m_instance;
       m_instance = NULL;
   }*/
}


////
//get config info in one line string
////
int getConfigOneLine(char * strIn,connection_config * stConnConfigOut)
{
	static const char * strRemoteIp = "remoteIp";
	static const char * strRemotePort = "remotePort";
	static const char * strLocalIp = "localIp";
	static const char * strLocalPort = "localPort";

	char * subStringStart = NULL;
	char * value = NULL;

	subStringStart = strstr(strIn,strRemoteIp);
	if(subStringStart)
	{
		struct in_addr tempInAddr;
		value = subStringStart +strlen(strRemoteIp) + 1;

		if(inet_pton(AF_INET,value,&tempInAddr) == 1)
		{
			stConnConfigOut->remoteAddr.sin_addr.s_addr = tempInAddr.s_addr;
			return 0;
		}
		else
		{
			return -1;
		}
	}

	subStringStart = strstr(strIn,strRemotePort);
	if(subStringStart)
	{
		uint16_t remotePort;
		value = subStringStart +strlen(strRemotePort) + 1;
		remotePort = (uint16_t)atoi(value);
		stConnConfigOut->remoteAddr.sin_port = htons(remotePort);

		return 0;
	}


	subStringStart = strstr(strIn,strLocalIp);
	if(subStringStart)
	{
		struct in_addr tempInAddr;
		value = subStringStart +strlen(strLocalIp) + 1;

		if(inet_pton(AF_INET, value,&tempInAddr) == 1)
		{
			stConnConfigOut->localAddr.sin_addr.s_addr = tempInAddr.s_addr;
			return 0;
		}
		else
		{
			return -1;
		}
	}

	subStringStart = strstr(strIn,strLocalPort);
	if(subStringStart)
	{
		uint16_t localPort;
		value = subStringStart + strlen(strLocalPort) + 1;
		localPort = (uint16_t)atoi(value);
		stConnConfigOut->localAddr.sin_port = htons(localPort);
	}

	return 0;
}

/////////////////////////
//get the net config from config file
///////////////////////////
int getConnectionConfig(connection_config * stConnConfig)
{
	char strOneLine[128] = {0};
    //char strIp[64] = {0};
    //unsigned char buffIp[4];

    FILE * fpConfig = NULL;

    fpConfig = fopen(configFileName,"r");
    if(fpConfig == NULL)
    {
    	printf("open config file failed\n");
    	return -1;
    }

    bzero(&stConnConfig->localAddr, sizeof(struct sockaddr_in));
    bzero(&stConnConfig->remoteAddr, sizeof(struct sockaddr_in));
    stConnConfig->localAddr.sin_family = AF_INET;
    stConnConfig->remoteAddr.sin_family = AF_INET;


    while(fgets(strOneLine,128,fpConfig))
    {
    	//printf("str one line: %s\n",strOneLine);
    	if((strOneLine[strlen(strOneLine)-1] == '\r')||(strOneLine[strlen(strOneLine)-1] == '\n'))
    	{
    		strOneLine[strlen(strOneLine)-1] = '\0';
    	}
    	getConfigOneLine(strOneLine,stConnConfig);
    }

    //memcpy(buffIp,&stConnConfig->remoteAddr.sin_addr.s_addr,4);
    //inet_ntop(AF_INET,buffIp,strIp,INET_ADDRSTRLEN);
    //printf("remoteIp:%s  reomtePort:%d\n",strIp,ntohs(stConnConfig->remoteAddr.sin_port));
    //memset(strIp,0,64);
    //inet_ntop(AF_INET,(char*)&(stConnConfig->localAddr.sin_addr.s_addr),strIp,INET_ADDRSTRLEN);
   //printf("localIp:%s  localPort:%d\n",strIp,ntohs(stConnConfig->localAddr.sin_port));


    fclose(fpConfig);
    fpConfig = NULL;

    return 0;
}




